package iap.dao;

import iap.model.Book;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class BookDAOImpl implements BookDAO {

	Connection connection;

	public BookDAOImpl(Connection connection) {
		super();
		this.connection = connection;
	}

	@Override
	public List<Book> getAllBooksFromDB() {
		// TODO Auto-generated method stub
		ArrayList<Book> resultList = new ArrayList<Book>();
		String queryText = "SELECT * FROM books;";
		ResultSet resultSet;
		Book book;
		try {
			Statement statement = connection.createStatement();
			resultSet = statement.executeQuery(queryText);
			while (resultSet.next()) {
				book = new Book();
				book.setIsbn(resultSet.getLong(1));
				book.setAuthor(resultSet.getString(2));
				book.setTitle(resultSet.getString(3));
				book.setPrice(resultSet.getInt(4));
				book.setAmountOnTheShell(resultSet.getInt(5));
				resultList.add(book);
			}
			statement.close();
			resultSet.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("We downloaded following number of books from DB : "
				+ resultList.size());
		return resultList;
	}

	@Override
	public void sellBookActionToDB(Book book) {
		// TODO Auto-generated method stub

	}

	@Override
	public void addNewBookToTheDB(Book book) {
		// TODO Auto-generated method stub
		String queryText = "INSERT INTO books"
				+ "(p_isbn, authorname, title, price, specimennumber) VALUES"
				+ "(?,?,?,?,?)";
		try {
			PreparedStatement preparedStatement = connection
					.prepareStatement(queryText);
			preparedStatement.setLong(1, book.getIsbn());
			preparedStatement.setString(2, book.getAuthor());
			preparedStatement.setString(3, book.getTitle());
			preparedStatement.setInt(4, book.getPrice());
			preparedStatement.setInt(5, book.getAmountOnTheShell());
			preparedStatement.executeUpdate();
			preparedStatement.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	public void updateBookAmountInDB(Book book) {
		// TODO Auto-generated method stub
		
		String queryText = "UPDATE books "
				+ "SET AuthorName = ?, Title = ?, Price = ?, SpecimenNumber = ? "
				+ "WHERE p_ISBN = ?";
		
		try {
			PreparedStatement preparedStatement = connection
					.prepareStatement(queryText);
			preparedStatement.setLong(5, book.getIsbn());
			preparedStatement.setString(1, book.getAuthor());
			preparedStatement.setString(2, book.getTitle());
			preparedStatement.setInt(3, book.getPrice());
			preparedStatement.setInt(4, book.getAmountOnTheShell());
			preparedStatement.executeUpdate();
			preparedStatement.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	

	@Override
	public void removeBookFromDB(Book book) {
		// TODO Auto-generated method stub
		String queryText = "DELETE FROM books WHERE p_ISBN = ?";

		PreparedStatement preparedStatement;
		try {
			preparedStatement = connection.prepareStatement(queryText);
			preparedStatement.setLong(1, book.getIsbn());
			preparedStatement.executeUpdate();
			preparedStatement.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
